Módulo:Ai
local ai = {} -------------------------------------------------- Used by Template:Ai -------------------------------------------------- function ai.main(frame) local args = frame:getParent().args local ability = args'1' or args'habilidad' or '' -- Cancel if missing ability if ability '' then return ERR('Ability name required!') end -- Passed Parameters local class = args'2' or args'clase' or '' local name = args'3' or args'nombre' or ability or '' local image = args'imagen' or ability local extra = tonumber(args'e') -- Change file path if basic attack if image 'Ataque Básico' and class ~= '' then local weapons = { 'Knight' = 'Melee', Neón' = 'Cuerpo a Cuerpo', Barbarian' = 'Melee', Bumerán' = {'Cuerpo a Cuerpo', 'Arco'}, 'Dracolyte' = 'Staff', Sage' = 'Staff', Trickster' = 'Staff', Raiser' = 'Staff', 'Cloromante' = 'Bastón', 'Pistolero' = 'Pistola', Captain' = 'Gun', Tamer' = 'Gun', 'Cazasombras' = 'Arco', Lancer' = 'Spear', 'Renacido' = 'Lanza', 'Vanguardián' = 'Puño' } local weapon = weaponsclassextra or weaponsclass1 or weaponsclass image = 'Styles_'..weapon end local size = args'tamaño' or '22' or '' local link = args'link' or linking(class, ability) local text = '' local span = mw.html.create('span') :css('white-space', 'pre') -- Removes tooltip if link is nil name = name:gsub('%%1', ability) if link ~= '' then if name ~= '' then text = string.format('%s', link, name):gsub('%%2', class) end if class ~= '' then span :addClass('custom-tooltip-ability') :attr('data-tooltip-class', class) :attr('data-tooltip-ability', ability) :attr('data-tooltip-extra', extra) :attr('data-tooltip-page', '#invoke:Ai|extract') end else text = name end ---------------------------------- span :wikitext(string.format(' ', image, size .. 'px', link)) :tag('span') :css('white-space', 'normal') :wikitext(text) :done() :done() return span end -- Doesn't generate link if class and link parameters are empty function linking(class, ability) if class ~= '' then if class 'Effect' then return ability else return mw.ustring.format('%s#Anchor_%s', class, ability) end else return '' end end -------------------------------------------------- Extract Template:Ability from a page. Used by Tooltips JS. -------------------------------------------------- function ai.extract(frame) local args = frame.args local ability = sanitize(args'ability') local class = sanitize(args'class') local extra = tonumber(args'extra') or 1 local result if class 'Effect' then result = mw.title.new(ability):getContent() or ERR('Page "%s" is empty or does not exist.', ability) return frame:preprocess(result) end local raw = mw.title.new(class):getContent() -- Error if page is non-existant if not raw then return ERR('Page "%s" is empty or does not exist.', class) end -------------------------------- local sortkey, value if ability 'Basic Attack' then sortkey = 'weapon' value = '.-' else sortkey = 'name' value = ability end result = {} for template in raw:gmatch('%b{}') do -- Removes comments template = template:gsub('', '') ------------------- if template:match(' ', '%1|tamaño=17}}'))) end end -- If no matches found if #result 0 then return ERR('"%s" does not exist in "%s".', ability, class) end ---------------------- result = resultextra or result1 return frame:preprocess(result:gsub('}}', '|format=tooltip\n}}')) end function sanitize(str) return str:gsub('([%(%)%.%+%-%*%?%%%^%$%%])', '%%%1') end function ERR(str, ...) local span = mw.html.create('span') :css({ 'color' = 'red', 'white-space' = 'pre', 'background-color' = 'black' }) :wikitext(string.format(str, ...)) return tostring(span) end return ai --[[ Iterator in case we ever need to pass template string to a table. local templates = {} local param = {} -- Get ability templates -- (^-)=(}+) Get template parameters -- sortkey..'%s-=%s+(}+)%s+') Get sortkey = ... parameter -- Get entire template. Doesn't work with nested templates. for word in string.gmatch(raw, ' ') do if word:find(sortkey) then if sortkey '|name' then if word:match(sortkey..'%s-=%s+(}+)%s+') ability then return word end else return word end end end ]]--